package service;

import com.alibaba.fastjson.JSON;
import mapper.ParentsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pojos.common.ReplyMessage;
import pojos.parents.ParentsInfo;
import pojos.parents.ParentsManagerInfo;
import pojos.subject.ClassInfoManager;
import utils.Md5Utils;
import utils.SpecialCodeReplaceUtil;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * 家长信息服务类
 */
public class ParentsInfoService {

    private final Logger logger = LoggerFactory.getLogger(ParentsInfoService.class);

    @Resource(name = "parentsMapper")
    private ParentsMapper parentsMapper;

    /**
     * 查询所有家长基本信息
     */
    public List<ParentsManagerInfo> queryAllParentsBaseInfo(){
        try {
            return parentsMapper.queryAllParentsBaseInfo();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询所有家长基本信息异常！");
            return null;
        }
    }

    /**
     * 添加家长信息
     */
    public ReplyMessage addParentsInfo(ParentsInfo parentsInfo){
        ReplyMessage replyMessage = new ReplyMessage();

        try {
            String nameRe = SpecialCodeReplaceUtil.doReplace(parentsInfo.getName());
            Integer isExist = parentsMapper.isLoginNameExist(nameRe);
            if(isExist != null && isExist > 0){
                replyMessage.setStatus(400);
                replyMessage.setInfo("登录名称已被占用，请更换其他登录名！");
                return replyMessage;
            }

            String passwordEn = Md5Utils.getMd5(parentsInfo.getPassword());
            parentsInfo.setPassword(passwordEn);
            parentsInfo.setName(nameRe);
            parentsInfo.setJoinTime(new Timestamp(System.currentTimeMillis()));

            parentsMapper.addParentsInfo(parentsInfo);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加家长信息异常，家长登录名："+parentsInfo.getName()+",登录密码："+parentsInfo.getPassword());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 查找所有的家长登录名、姓名
     */
    public ReplyMessage queryAllParentsName(){
        ReplyMessage replyMessage = new ReplyMessage();

        try {
            List<ParentsInfo> parentsInfoList = parentsMapper.queryAllParentsName();
            if(parentsInfoList != null){
                replyMessage.setStatus(200);
                replyMessage.setInfo(JSON.toJSONString(parentsInfoList));
                return replyMessage;
            }
            replyMessage.setStatus(400);
            replyMessage.setInfo("查询结果为空！");
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查找所有的家长姓名异常");

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 家长登录检查
     */
    public ReplyMessage parentsLoginCheck(String userName, String password){
        ReplyMessage replyMessage = new ReplyMessage();

        try {
            String userNameRe = SpecialCodeReplaceUtil.doReplace(userName);
            ParentsInfo parentsInfo = parentsMapper.loginCheck(userNameRe);

            if(parentsInfo == null){
                replyMessage.setStatus(400);
                replyMessage.setInfo("用户不存在，请检查用户名！");
                return replyMessage;
            }

            if(parentsInfo.getAdminLock() != 0){
                replyMessage.setStatus(400);
                replyMessage.setInfo("账号被限制登录，请联系客服了解详情");
                return replyMessage;
            }

            if(parentsInfo.getSysLock() != 0){
                replyMessage.setStatus(400);
                replyMessage.setInfo("今天之内密码错误次数太多，为保护账号安全，账号已被锁定，请在24小时后重试或联系客服解决");
                return replyMessage;
            }

            String passwordEn = Md5Utils.getMd5(password);
            if(parentsInfo.getPassword().equals(passwordEn)){
                parentsInfo.setPassword("");
                parentsInfo.setName(userName);
                replyMessage.setStatus(200);
                replyMessage.setObject(parentsInfo);
                return replyMessage;
            }

            // 锁定账户
            var errorTime = parentsInfo.getErrorTime();
            var parentsId = parentsInfo.getId();
            if(errorTime > 10){
                parentsMapper.sysLockParents(parentsId);
                parentsMapper.sysAutoUnLock(parentsId);
            }
            parentsMapper.updateErrorTime(parentsId);

            replyMessage.setStatus(400);
            replyMessage.setInfo("密码错误！");
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("家长登录异常，登录名："+userName+"，登录密码："+password);

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 根据登录名称查找手机号
     */
    public ParentsInfo queryPhoneByName(String name,String phone){
        String nameRe = SpecialCodeReplaceUtil.doReplace(name);
        try {
            ParentsInfo parentsInfo = parentsMapper.queryPhoneByName(nameRe);
            if(parentsInfo != null){
                if(parentsInfo.getPhone().equals(phone)){
                    return parentsInfo;
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("根据家长登录名称查找手机号异常，家长登录名称："+name);

            return null;
        }
    }

    /**
     * 家长修改基础信息
     */
    public ReplyMessage parentsAlterBaseInfo(ParentsInfo parentsInfo){
        String realNameRe = SpecialCodeReplaceUtil.doReplace(parentsInfo.getRealName());
        parentsInfo.setRealName(realNameRe);

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            parentsMapper.alterBaseInfo(parentsInfo);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("家长修改基础信息异常，家长ID："+parentsInfo.getId());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 家长修改登录名
     */
    public ReplyMessage alterLoginName(ParentsInfo parentsInfo){
        String nameRe = SpecialCodeReplaceUtil.doReplace(parentsInfo.getName());
        parentsInfo.setName(nameRe);

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            Integer isNameExist = parentsMapper.isLoginNameExist(nameRe);
            if(isNameExist != null && isNameExist > 0){
                replyMessage.setStatus(400);
                replyMessage.setInfo("登录名称已经被占用，请更换其他名称！");
                return replyMessage;
            }

            parentsMapper.alterLoginName(parentsInfo);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改家长登录名称异常，家长ID："+parentsInfo.getId());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 家长修改登录密码
     */
    public ReplyMessage alterPassword(String oldPassword,String newPassword,ParentsInfo parentsInfo){
        ReplyMessage replyMessage = new ReplyMessage();

        try {
            String queryPassword = parentsMapper.queryPasswordById(parentsInfo.getId());
            if(queryPassword == null || queryPassword.equals("")){
                replyMessage.setStatus(400);
                replyMessage.setInfo("服务器异常，请稍后重试！");
                return replyMessage;
            }

            String oldPasswordEn = Md5Utils.getMd5(oldPassword);
            if(queryPassword.equals(oldPasswordEn)){
                String newPasswordEn = Md5Utils.getMd5(newPassword);
                parentsInfo.setPassword(newPasswordEn);
                parentsMapper.alterPassword(parentsInfo);

                replyMessage.setStatus(200);
                return replyMessage;
            }
            replyMessage.setStatus(400);
            replyMessage.setInfo("原密码不正确！");
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("家长修改密码异常，家长ID："+parentsInfo.getId());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 家长修改手机号
     */
    public ReplyMessage alterPhone(ParentsInfo parentsInfo){
        ReplyMessage replyMessage = new ReplyMessage();

        try {
            parentsMapper.alterPhone(parentsInfo);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改家长手机号异常，请稍后重试！");

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 修改家长联系信息
     */
    public ReplyMessage alterConnInfo(ParentsInfo parentsInfo){
        String weChatRe = SpecialCodeReplaceUtil.doReplace(parentsInfo.getWeChat());
        parentsInfo.setWeChat(weChatRe);

        ReplyMessage replyMessage = new ReplyMessage();
        try {
            parentsMapper.alterConnectInfo(parentsInfo);
            replyMessage.setStatus(200);
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改家长联系信息异常，家长ID："+parentsInfo.getId());

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }

    /**
     * 家长查找当天课程信息
     */
    public ReplyMessage queryParentsCurrentClassInfo(int parentsId){
        ReplyMessage replyMessage = new ReplyMessage();

        try {
            List<ClassInfoManager> classInfoManagerList = parentsMapper.queryCurrentClassInfo(parentsId);
            if(classInfoManagerList != null){
                replyMessage.setStatus(200);
                replyMessage.setInfo(JSON.toJSONString(classInfoManagerList));
                return replyMessage;
            }
            replyMessage.setStatus(400);
            replyMessage.setInfo("查询结果为空！");
            return replyMessage;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("家长查询当天课程信息异常，家长ID："+ parentsId);

            replyMessage.setStatus(500);
            replyMessage.setInfo("服务器异常，请稍后重试！");
            return replyMessage;
        }
    }
}
